package com.renwei.yunlong.view;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.renwei.yunlong.R;
import com.scwang.smartrefresh.layout.util.DensityUtil;

import io.rong.imkit.emoticon.AndroidEmoji;
import io.rong.imkit.model.ProviderTag;
import io.rong.imkit.model.UIMessage;
import io.rong.imkit.widget.provider.IContainerItemProvider;

/**
 * Created by wangqi on 2018/5/2.
 */
@ProviderTag(messageContent = NoticeMessage.class,showPortrait = false,centerInHorizontal = true)
public class NoticeMessageItemProvider extends IContainerItemProvider.MessageProvider<NoticeMessage> {

    private Context context;

    public NoticeMessageItemProvider(Context context) {
        this.context = context;
    }

    class ViewHolder {
        TextView tvTitle;
        TextView tvContent;
        LinearLayout llMessage;
        RelativeLayout rlEnter;
    }

    @Override
    public View newView(Context context, ViewGroup viewGroup) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_notice_msg_view, viewGroup, false);
        ViewHolder holder = new ViewHolder();
        holder.tvTitle = view.findViewById(R.id.tv_title);
        holder.tvContent = view.findViewById(R.id.tv_content);
        holder.llMessage = view.findViewById(R.id.ll_message);
        holder.rlEnter = view.findViewById(R.id.rl_enter);
        view.setTag(holder);
        return view;
    }

    @Override
    public void bindView(View view, int i, NoticeMessage messageContent, UIMessage message) {
        ViewHolder holder = (ViewHolder) view.getTag();
//        if (message.getMessageDirection() == Message.MessageDirection.SEND) {//消息方向，自己发送的
//            holder.llMessage.setBackgroundResource(io.rong.imkit.R.drawable.rc_ic_bubble_right);
//        } else {
//            holder.llMessage.setBackgroundResource(io.rong.imkit.R.drawable.rc_ic_bubble_left);
//        }
        holder.tvTitle.setText(messageContent.getTitle(),TextView.BufferType.SPANNABLE);
        AndroidEmoji.ensure((Spannable) holder.tvTitle.getText());//显示消息中的 Emoji 表情。

        holder.tvContent.setText(messageContent.getContent(),TextView.BufferType.SPANNABLE);
        AndroidEmoji.ensure((Spannable) holder.tvContent.getText());//显示消息中的 Emoji 表情。

        if (!TextUtils.isEmpty(messageContent.getUrl())){
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, DensityUtil.dp2px(44));
            holder.rlEnter.setLayoutParams(params);
            holder.rlEnter.setVisibility(View.VISIBLE);
        }else{
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0);
            holder.rlEnter.setLayoutParams(params);
            holder.rlEnter.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public Spannable getContentSummary(NoticeMessage messageContent) {
        return new SpannableString("公告");
    }

    @Override
    public void onItemClick(View view, int i, NoticeMessage messageContent, UIMessage uiMessage) {
        if (!TextUtils.isEmpty(messageContent.getUrl())){
            WebBrowserActivity.openActivity(context, messageContent.getUrl());
        }
    }
}
